home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / findpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-10  |  2.8 KB  |  100 lines  |  [TEXT/CWIE]

  1. /*
  2.     Loki_Update - A tool for updating Loki products over the Internet
  3.     Copyright (C) 2000  Loki Software, Inc.
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     info@lokigames.com
  20. */
  21.  
  22. /* I modified this file a little, removing unneeded features and making the
  23.    thing compile */
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29.  
  30. #define PATH_MAX 8192
  31.  
  32. #ifndef WIN32
  33. void goto_installpath(char *argv0)
  34. {
  35.     char temppath[PATH_MAX];
  36.     char datapath[PATH_MAX];
  37.     char *home;
  38.  
  39.     home = getenv("HOME");
  40.     if ( ! home ) {
  41.         home = ".";
  42.     }
  43.  
  44.     strcpy(temppath, argv0);    /* If this overflows, it's your own fault :) */
  45.     if ( ! strrchr(temppath, '/') ) {
  46.         char *path;
  47.         char *last;
  48.         int found;
  49.  
  50.         found = 0;
  51.         path = getenv("PATH");
  52.         do {
  53.             /* Initialize our filename variable */
  54.             temppath[0] = '\0';
  55.  
  56.             /* Get next entry from path variable */
  57.             last = strchr(path, ':');
  58.             if ( ! last )
  59.                 last = path+strlen(path);
  60.  
  61.             /* Perform tilde expansion */
  62.             if ( *path == '~' ) {
  63.                 strcpy(temppath, home);
  64.                 ++path;
  65.             }
  66.  
  67.             /* Fill in the rest of the filename */
  68.             if ( last > (path+1) ) {
  69.                 strncat(temppath, path, (last-path));
  70.                 strcat(temppath, "/");
  71.             }
  72.             strcat(temppath, "./");
  73.             strcat(temppath, argv0);
  74.  
  75.             /* See if it exists, and update path */
  76.             if ( access(temppath, X_OK) == 0 ) {
  77.                 ++found;
  78.             }
  79.             path = last+1;
  80.  
  81.         } while ( *last && !found );
  82.  
  83.     } else {
  84.         /* Increment argv0 to the basename */
  85.         argv0 = strrchr(argv0, '/')+1;
  86.     }
  87.  
  88.     /* Now canonicalize it to a full pathname for the data path */
  89.     datapath[0] = '\0';
  90.     if ( realpath(temppath, datapath) ) {
  91.         /* There should always be '/' in the path */
  92.         *(strrchr(datapath, '/')) = '\0';
  93.     }
  94.     if ( ! *datapath || (chdir(datapath) < 0) ) {
  95.         fprintf(stderr, "Couldn't change to install directory\n");
  96.         exit(1);
  97.     }
  98. }
  99. #endif
  100.